## Figure 1: 311 Usage by Neighborhood Race and Income

## install.packages(c("tidyverse", "patchwork", "ggrepel"))
## library(tidyverse)
## library(patchwork)
## library(ggrepel)

## SET WORKING DIRECTORY HERE
## setwd()

## Loading data
## load("dta.RData")

## Prepping
geo_pop = dta |> 
  distinct(geo, total_pop)

dta_percap_c = dta |> 
  group_by(geo, city, per_cap_inc, pct_white) |> 
  summarise(tot_calls = n()) |> 
  ungroup() |> 
  left_join(geo_pop) |> 
  mutate(call_percap = tot_calls / total_pop)

## Figure 1a
race_call_percap_c = dta_percap_c |> 
  ggplot(aes(x = pct_white, y = call_percap)) +
  geom_point(aes(size = log(total_pop)),
             alpha = 0.2) +
  geom_smooth(method = "lm",
              aes(weight = total_pop)) +
  facet_wrap(~ city, scales = "free_y", ncol = 2) +
  theme_classic() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black"),
        legend.position = "bottom") +
  labs(x = "% White",
       y = "Mean Calls Per Capita") +
  guides(size= guide_legend(title = "ln(Population)"))

## Figure 1b
inc_call_percap_c = dta_percap_c |> 
  mutate(log_pci = log(per_cap_inc)) |> 
  ggplot(aes(x = log_pci, y = call_percap)) +
  geom_point(aes(size = log(total_pop)),
             alpha = 0.2) +
  geom_smooth(method = "lm",
              aes(weight = total_pop)) +
  facet_wrap(~ city, scales = "free", ncol = 2) +
  theme_classic() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black"),
        legend.position = "bottom") +
  labs(x = "ln(Per Capita Income)",
       y = "Mean Calls Per Capita") +
  guides(size= guide_legend(title = "ln(Population)"))